package pld.car.service.impl;

import java.util.List;

import pld.car.bean.News;
import pld.car.bean.NewsImg;
import pld.car.service.*;

public class NewsServiceImpl extends BaseServiceImpl implements NewsService {

	public NewsServiceImpl() {
		System.out.println("NewsServiceImpl init!");
	}

	// 获取指定数量 客运信息 的标题和时间
	public List<Object> getKYXXTitle(int first, int number) {
		String sql = "select id, title, createDate from News where type='kyxx' order by id desc";
		return this.getNewsDao().runSQL(sql, first, number);
	}

	// 获取指定数量 行业动态 的标题和时间
	public List<Object> getHYDTTitle(int first, int number) {
		String sql = "select id, title, createDate from News where type='hydt' order by id desc";
		return this.getNewsDao().runSQL(sql, first, number);
	}

	// 获取news表的记录数
	public int getNewsCount() {
		long count;
		String sql = "select count(*) from News";
		List<Object> ob = this.getNewsDao().runSQL(sql);
		count = (Long) ob.get(0);
		return (int) count;
	}

	// 获取news表中客运信息的记录数
	public int getKYXXCount() {
		long count;
		String sql = "select count(*) from News where type='kyxx'";
		List<Object> ob = this.getNewsDao().runSQL(sql);
		count = (Long) ob.get(0);
		return (int) count;
	}

	// 获取news表中行业动态的记录数
	public int getHYDTCount() {
		long count;
		String sql = "select count(*) from News where type='hydt'";
		List<Object> ob = this.getNewsDao().runSQL(sql);
		count = (Long) ob.get(0);
		return (int) count;
	}

	// 获取指定起始记录和数量的数据
	public List<Object> getNewsTitle(int first, int number) {
		String sql = "select id, title, author, createDate, lastModified, type from News order by id desc";
		return this.getNewsDao().runSQL(sql, first, number);
	}

	// 根据id删除指定的新闻
	public void delete(int id) {
		this.getNewsDao().delete(id);
	}

	// 保存新闻
	public void insert(News o) {

		this.getNewsDao().insert(o);
	}

	// 修改新闻
	public void update(News p) {
		this.getNewsDao().update(p);
	}

	// 根据id查询到指定的新闻
	public News findNewsByID(int id) {
		return this.getNewsDao().findNewsByID(id);
	}

	// 根据ID查到对应的图片
	public NewsImg findNewsImgByID(int id) {
		return this.getNewsDao().findImgByID(id);
	}

	// 删除newsImg表中的没有imgID的记录
	public void deleteOrphanItems(){
		this.getNewsDao().deleteOrphanItems();
	}
}
